package puppy.graph;

import java.awt.Dimension;
import java.awt.Paint;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import util.io.FileInput;

public class CreateGraph {

	private static final Integer EDGE_THRESHOLD = 3;
	private static final Integer NODE_THRESHOLD = 3;
	
	
	private static final Integer EDGE_THRESHOLD_KIDS = 2;
	private static final Integer NODE_THRESHOLD_KID = 3;
	
	
	/**
	 * @param args
	 */
	public int n_nodes = 0;
	public int n_edges = 0;

	// Generate
	// id_url, id_tag, url,tag,freq_tag,freq_url, freq_ut,type
	
	
	//http://5.ua/	tv_news	adults	11953	10	3	5.ua

	public UndirectedSparseGraph<DeliciousNode, DeliciousEdge> graph = null;
	public DirectedSparseGraph<DeliciousNode, DeliciousEdge> graph_d= null;
	
	
	private int _id_url = 0;
	private int _id_tag = 1;
	private int _url = 2;
	private int _tag = 3;
	private int _freq_u = 5;
	private int _freq_tag = 4;
	private int _freq_ut = 6;
	private int _age = 7;

	public Hashtable<Integer, DeliciousNode> nodes = new Hashtable<Integer, DeliciousNode>();
	public Hashtable<String, DeliciousEdge> edges = new Hashtable<String, DeliciousEdge>();
	//HashSet<Integer> vertexes = new HashSet<Integer>();

	Hashtable<String,Integer> urls_totals = new Hashtable<String,Integer>();
	
	Hashtable<String,Integer> tags_totals = new Hashtable<String,Integer>();
	
	
	// Hashtable<String, Integer> dict = new Hashtable<String, Integer>();

	/*
	 * public void initNodes(String path) {
	 * 
	 * FileInput in = new FileInput(path); int id = 0; String line =
	 * in.readString();
	 * 
	 * 
	 * while (line != null) {
	 * 
	 * String fields[] = line.split("\t"); if (fields.length > 3) {
	 * 
	 * String url = fields[2]; String tag = fields[3];
	 * 
	 * //System.out.println(url + "\t" + tag); if(!dict.containsKey(url)){
	 * dict.put(url, id); id++;
	 * 
	 * }
	 * 
	 * if(!dict.containsKey(tag)){ dict.put(tag, id); id++; } document language
	 * model query models and risk minimization } System.out.println(id); //
	 * System.out.println(line); line = in.readString(); } }
	 */

	public void initGraph(String path) {

		FileInput in = new FileInput(path);
		graph = new UndirectedSparseGraph<DeliciousNode, DeliciousEdge>();
		String line = in.readString();
		
		int j =0;
		while (line != null) {
			System.out.println("Adding " + line);
			dumpLineIntoGraph(graph, line);
			line = in.readString();
			j++;
			//if(j==5) return;
		}
	}

	private void dumpLineIntoGraph(
			UndirectedSparseGraph<DeliciousNode, DeliciousEdge> graph,
			String line) {

		n_nodes++;
		n_edges++;

		String[] fields = line.split("\t");

		Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
		Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

		Integer freq_tag = Integer.valueOf(Integer.valueOf(fields[_freq_tag]));
		Integer freq_url = Integer.valueOf(Integer.valueOf(fields[_freq_u]));
		Integer freq_ut = Integer.valueOf(Integer.valueOf(fields[_freq_ut]));
		
		String age = fields[_age];
		String name = fields[_tag];

		
		//if(freq_ut<EDGE_THRESHOLD ){
		//	return;
		//}
		
		
		
		
		/*
		 * Adding url node
		 */
		if (nodes.containsKey(id_url)) {
			DeliciousNode url = nodes.get(id_url);
			url.setFreq(freq_url, age);
			nodes.put(id_url, url);
		} else {
			DeliciousNode url = new DeliciousNode();
			url.setFreq(freq_url, age);
			nodes.put(id_url, url);

		}

		/**
		 * Adding tag node
		 * 
		 */

		if (nodes.containsKey(id_tag)) {
			DeliciousNode tag = nodes.get(id_tag);
			tag.setFreq(freq_tag, age);
			nodes.put(id_tag, tag);
			

		} else {
			DeliciousNode tag = new DeliciousNode(Integer.valueOf(id_tag));
			tag.setFreq(freq_tag, age);
			tag.setName(name);
			nodes.put(id_tag, tag);

		}

		/*
		 * Adding edge
		 */

		String edge_key = id_url + "\t" + id_tag;

		if (edges.containsKey(edge_key)) {
			DeliciousEdge edge = edges.get(edge_key);

			edge.setFreq(freq_ut, age);
			edges.put(edge_key, edge);

		} else {
			DeliciousEdge edge = new DeliciousEdge();
			edge.setFreq(freq_ut, age);
			edges.put(edge_key, edge);

		}

		

		// TODO Auto-generated method stub

	}
	
	
	public void createGraph(){
		

		/** add nodes 
		 * 
		 * 
		 */
		
		Enumeration<Integer> keys = nodes.keys();
		
		while(keys.hasMoreElements()){
			
			Integer key = keys.nextElement();
			DeliciousNode node = nodes.get(key);
			node.setId(key);
			graph.addVertex(node); 
			 
			 n_nodes++;
			
		}
		
		
		/**
		 * add edges
		 * 
		 */
		
		Enumeration<String> keys2 = edges.keys();
		
		
		while(keys2.hasMoreElements()){
			
			
			String key2= keys2.nextElement();
			DeliciousEdge edge = edges.get(key2);
			
			String ids[]= key2.split("\t");
			//System.out.println("Edge:" + );
			
			DeliciousNode url = nodes.get(Integer.valueOf(ids[0]));
			DeliciousNode tag = nodes.get(Integer.valueOf(ids[1]));
			
			graph.addEdge(edge, url,tag);
			
			n_edges++;
			
		}

		  System.out.println("Edges:  " + n_edges);
		  System.out.println("Nodes:  " + n_nodes);
		
	}

	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String prefix = "/Users/sergioduarte/projects/data/graph/";
		
		//String path = "/media/sata__/data/delicious_graph/delicious_graphML_raw_ids_dmoz.txt";
		String path = prefix+ "delicious_graphML_raw_ids_dmoz_youth_clean_kids.txt";
		
		CreateGraph graph = new CreateGraph();

		graph.initGraph(path);
		graph.createGraph();
		HashSet<DeliciousNode> set = new HashSet<DeliciousNode>();
		GraphExtractor extractor= new GraphExtractor(1,set);
		
		Layout<Integer, String> layout = new CircleLayout(graph.graph);
	    layout.setSize(new Dimension(300,300)); // sets the initial size of the space
	     // The BasicVisualizationServer<V,E> is parameterized by the edge types
	     BasicVisualizationServer<Integer,String> vv =
	              new BasicVisualizationServer<Integer,String>(layout);
	     vv.setPreferredSize(new Dimension(350,350)); //Sets the viewing area size
	     JFrame frame = new JFrame("Simple Graph View");
	     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	     frame.getContentPane().add(vv);
	     frame.pack();
	     frame.setVisible(true);
		     
		 
		

	}

}
